﻿using GTI = Gadgeteer.Interfaces;
using GTM = Gadgeteer.Modules;

namespace Gadgeteer.Modules.DFRobot
{
    /// <summary>
    ///     A UV Sensor module for Microsoft .NET Gadgeteer
    /// </summary>
    public class UV_Sensor : Module
    {
        // This example implements a driver in managed code for a simple Gadgeteer module.  This module uses a 
        // single GTI.InterruptInput to interact with a button that can be in either of two states: pressed or released.
        // The example code shows the recommended code pattern for exposing a property (IsPressed). 
        // The example also uses the recommended code pattern for exposing two events: Pressed and Released. 
        // The triple-slash "///" comments shown will be used in the build process to create an XML file named
        // GTM.DFRobot.UV_Sensor. This file will provide IntelliSense and documentation for the
        // interface and make it easier for developers to use the UV Sensor module.        

        // Note: A constructor summary is auto-generated by the doc builder.

        private readonly GTI.AnalogInput input;

        /// <summary></summary>
        /// <param name="socketNumber">The socket that this module is plugged in to.</param>
        public UV_Sensor(int socketNumber)
        {
            // This finds the Socket instance from the user-specified socket number.  
            // This will generate user-friendly error messages if the socket is invalid.
            // If there is more than one socket on this module, then instead of "null" for the last parameter, 
            // put text that identifies the socket to the user (e.g. "S" if there is a socket type S)
            Socket socket = Socket.GetSocket(socketNumber, true, this, null);

            socket.EnsureTypeIsSupported('A', this);

            input = new GTI.AnalogInput(socket, Socket.Pin.Four, this);
        }

        /// <summary>
        ///     Returns the current voltage reading of the UV sensor
        /// </summary>
        public double ReadUVSensorVoltage()
        {
            return input.ReadVoltage();
        }

        /// <summary>
        ///     Returns the current UV Level reading of the UV sensor
        /// </summary>
        public double GetUVI()
        {
            double volts = ReadUVSensorVoltage();

            double UVI = (((volts*3300)/4.3) - 45)/27.0;

            if (UVI < 0)
                return 0.0;
            else
                return UVI;
        }
    }
}